/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ExportDossard.java
 *
 * Created on Dec 9, 2011, 12:13:46 AM
 */
package eyeofthetiger.gui;

import eyeofthetiger.model.Course;
import eyeofthetiger.model.Participant;
import eyeofthetiger.model.Project;
import eyeofthetiger.utils.PDFDossardGenerator;
import eyeofthetiger.utils.PDFDossardGenerator.SortBy;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author christophe
 */
public class ExportDossard extends javax.swing.JFrame {

    protected static final ResourceBundle MSGS = ResourceBundle.getBundle(MBAction.classBundleBaseName(ExportDossard.class));
    
    class ComboItem {
        ComboItem(List<Participant> listParticipant, String label, String projectOrCourseName) {
            this.label = label;
            this.listParticipant = listParticipant;
            this.projectOrCourseName = projectOrCourseName;
        }
        
        List<Participant> listParticipant;
        String label;
        String projectOrCourseName;
        
        public String toString() {
            return label;
        }
    }

    

    private static SortBy lastSortByChoice = SortBy.alphabetique;
    
    private Project project;
    
    /** Creates new form ExportDossard */
    public ExportDossard(Project p, List<Participant> customSelection) {
        project = p;
        initComponents();
        
        jComboBox.removeAllItems();
        
        for(Course c : project.getCourse()) {
            jComboBox.insertItemAt(new ComboItem(c.getParticipants(), "Tous les participants de la course " + c.getName(), c.getName()), 0);
        }
        jComboBox.insertItemAt(new ComboItem(project.getParticipants(), "Tous les participants du projet", project.getName()), 0);
        
        if(customSelection != null && customSelection.size()>0) {
            jComboBox.insertItemAt(new ComboItem(customSelection, "Selection courante (" + customSelection.size() + " participants)", project.getName()+"_selection"), 0);
        }
        
        jComboBox.setSelectedIndex(0);
        
        jComboBoxSortBy.addItem(SortBy.alphabetique);
        jComboBoxSortBy.addItem(SortBy.inscriptionDate);
        jComboBoxSortBy.addItem(SortBy.numero);
        jComboBoxSortBy.addItem(SortBy.groupe);
        jComboBoxSortBy.addItem(SortBy.renseignement);
        jComboBoxSortBy.setSelectedItem(lastSortByChoice);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButtonExport = new javax.swing.JButton();
        jComboBox = new javax.swing.JComboBox();
        jCheckBoxIncludeName = new javax.swing.JCheckBox();
        jCheckBoxIncludeGroup = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        jCheckBoxIncludeRenseignements = new javax.swing.JCheckBox();
        jLabel2 = new javax.swing.JLabel();
        jComboBoxSortBy = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jButtonExport.setText(MSGS.getString("jButtonExport.text")); // NOI18N
        jButtonExport.setName("jButtonExport"); // NOI18N
        jButtonExport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonExportActionPerformed(evt);
            }
        });

        jComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox.setName("jComboBox"); // NOI18N

        jCheckBoxIncludeName.setSelected(true);
        jCheckBoxIncludeName.setText(MSGS.getString("jCheckBoxIncludeName.text")); // NOI18N
        jCheckBoxIncludeName.setName("jCheckBoxIncludeName"); // NOI18N

        jCheckBoxIncludeGroup.setSelected(true);
        jCheckBoxIncludeGroup.setText(MSGS.getString("jCheckBoxIncludeGroup.text")); // NOI18N
        jCheckBoxIncludeGroup.setName("jCheckBoxIncludeGroup"); // NOI18N

        jLabel1.setText(MSGS.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jCheckBoxIncludeRenseignements.setSelected(true);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("gui/resources/ExportDossard"); // NOI18N
        jCheckBoxIncludeRenseignements.setText(bundle.getString("ExportDossard.jCheckBoxIncludeRenseignements.text")); // NOI18N
        jCheckBoxIncludeRenseignements.setName("jCheckBoxIncludeRenseignements"); // NOI18N

        jLabel2.setText(bundle.getString("ExportDossard.jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jComboBoxSortBy.setName("jComboBoxSortBy"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBoxSortBy, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
                        .addComponent(jButtonExport))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jCheckBoxIncludeGroup)
                            .addComponent(jCheckBoxIncludeName)
                            .addComponent(jLabel1)
                            .addComponent(jCheckBoxIncludeRenseignements))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jCheckBoxIncludeName)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBoxIncludeGroup)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBoxIncludeRenseignements)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBoxSortBy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonExport))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButtonExportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExportActionPerformed
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setSelectedFile(new File(project.getPath(), ((ComboItem)jComboBox.getSelectedItem()).projectOrCourseName + ".pdf"));
    FileFilter pdfFileFilter = new FileNameExtensionFilter("Fichiers PDF", "pdf");
    fileChooser.addChoosableFileFilter(pdfFileFilter);
    fileChooser.setAcceptAllFileFilterUsed(true);
    fileChooser.setFileFilter(pdfFileFilter);
    if(fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();
        FileOutputStream out;
        try {
            out = new FileOutputStream(selectedFile);
            ComboItem selectedIem = ((ComboItem)jComboBox.getSelectedItem());
            
            PDFDossardGenerator pdfGenertor = new PDFDossardGenerator();
            lastSortByChoice = (SortBy)jComboBoxSortBy.getSelectedItem();
            pdfGenertor.setSortBy(lastSortByChoice);
            pdfGenertor.setExportName(jCheckBoxIncludeName.isSelected());
            pdfGenertor.setExportGroup(jCheckBoxIncludeGroup.isSelected());
            pdfGenertor.setExportRenseignement(jCheckBoxIncludeRenseignements.isSelected());
            pdfGenertor.setMarginCm(project.getOptions().getMarginCm());
            pdfGenertor.setLogoLeft(new File(project.getPath(),project.getOptions().getLogoLeft()).getAbsolutePath());
            pdfGenertor.setLogoRight(new File(project.getPath(),project.getOptions().getLogoRight()).getAbsolutePath());
            pdfGenertor.setPdfBackground(new File(project.getPath(),project.getOptions().getPdfBackground()).getAbsolutePath());
            pdfGenertor.setLogoLeftWidth(project.getOptions().getLogoLeftWidth());
            pdfGenertor.setLogoRightWidth(project.getOptions().getLogoRightWidth());
            pdfGenertor.createPdf(selectedIem.listParticipant,out);
            //PDFUtils.createPdf(selectedIem.listParticipant, jCheckBoxIncludeName.isSelected(), jCheckBoxIncludeGroup.isSelected(), jCheckBoxIncludeRenseignements.isSelected(), out);
            out.close();
        }
        catch(Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,"Erreur lors de la création du pdf des dossards: " + e.getMessage() ,"Erreur",JOptionPane.ERROR_MESSAGE);
        }        
    }
}//GEN-LAST:event_jButtonExportActionPerformed




    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonExport;
    private javax.swing.JCheckBox jCheckBoxIncludeGroup;
    private javax.swing.JCheckBox jCheckBoxIncludeName;
    private javax.swing.JCheckBox jCheckBoxIncludeRenseignements;
    private javax.swing.JComboBox jComboBox;
    private javax.swing.JComboBox jComboBoxSortBy;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables
}
